package com.cys.dm.controller;

import com.cys.dm.entity.order.DmOrderLinkUser;
import com.cys.dm.entity.user.DmLinkUser;
import com.cys.dm.rest.service.DmOrderLinkUserService;
import com.cys.dm.rest.service.DmOrderService;
import com.cys.dm.vo.CommonResponse;
import com.cys.dm.vo.UserTokenResponse;
import com.cys.dm.vo.VoUtil;
import com.cys.dm.vo.order.reception.OrderDscReception;
import com.cys.dm.vo.order.reception.OrderReception;
import com.cys.dm.vo.order.response.OrderResponse;
import com.cys.dm.vo.user.QueryPersonInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author blue_t
 * @date 2020-12-23 17:19
 */
@RestController
public class DmOrderController {
    @Autowired
    private DmOrderLinkUserService dmOrderLinkUserService;
    @Autowired
    private DmOrderService dmOrderService;

    /**
     * 根据用户Id查询常用联系人
     * @param map
     * @return
     */
    @PostMapping(path = {"/ticketbuyerlist"})
    public CommonResponse<List<DmLinkUser>> ticketbuyerlist(@RequestBody Map<String,Map<String,Object>> map){
        return VoUtil.returnSuccess("获取常用联系人成功！",dmOrderLinkUserService.ticketbuyerlist(map));
    }

    /**
     *验证购票人是否已经存在
     * @param map
     * @return
     */
    @PostMapping(path = "/validatebuyerexist")
    public CommonResponse<String> validatebuyerexist(@RequestBody Map<String,Long> map){
        return VoUtil.returnSuccess("常用联系人验证完毕",dmOrderLinkUserService.queryLinkUserByIdCard(map));
    }

    /**
     * 添加常用购票人
     * @param map
     * @return
     */
    @PostMapping(path = "/addticketbuyer")
    public CommonResponse<Integer> addticketbuyer(@RequestBody Map<String,Object> map){
        return VoUtil.returnSuccess("添加常用联系人成功",dmOrderLinkUserService.addLinkUser(map));
    }

    /*@PostMapping(path = "/deleteticketbuyer")
    public CommonResponse<Integer> deleteticketbuyer(){

    }*/

    /**
     * 下单
     * @param orderReception
     * @return
     */
    @PostMapping(path = "/submitorder")
    public CommonResponse<String> submitorder(@RequestBody OrderReception orderReception){
        String orderNo = dmOrderService.submitorder(orderReception);
        System.out.println("订单编号------"+orderNo);
        return VoUtil.returnSuccess("下单成功",orderNo);
    }

    /**
     * 根据条件查询信息
     * @param request
     * @param params
     * @return
     */
    @PostMapping(path = "/queryorderlist")
    public CommonResponse<List<OrderResponse>> queryorderlist(HttpServletRequest request, @RequestBody OrderDscReception params){
        return VoUtil.returnSuccess("订单查询",dmOrderService.queryorderlist(request,params));
    }

    /**
     * 查询当前用户信息
     * @param map
     * @return
     */
    @PostMapping(path = "/querypersoninfo")
    public CommonResponse<QueryPersonInfoVo> querypersoninfo(@RequestBody Map<String,Long> map){
        return VoUtil.returnSuccess("用户信息",dmOrderService.querypersoninfo(map));
    }

    /**
     * 需改用户信息
     * @param request
     * @param response
     * @param queryPersonInfoVo
     * @return
     * @throws IOException
     */
    @PostMapping(path = "/modifypersoninfo")
    public CommonResponse<UserTokenResponse> modifypersoninfo(HttpServletRequest request,
                                                              HttpServletResponse response,
                                                              @RequestBody QueryPersonInfoVo queryPersonInfoVo) throws IOException {
        return VoUtil.returnSuccess("修改用户信息成功",dmOrderService.modifypersoninfo(request,response,queryPersonInfoVo));
    }

}
